<template>
    <div>
<el-card>
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['super:coupon:add']"
          >新增</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="success"
            icon="el-icon-edit"
            size="mini"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['super:coupon:edit']"
          >修改</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="danger"
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['super:coupon:remove']"
          >删除</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="warning"
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
            v-hasPermi="['super:coupon:export']"
          >导出</el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>

      <el-table v-loading="loading" :data="couponList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="平台优惠卷" align="center" prop="platformId" />
        <!--<el-table-column label="店铺ID" align="center" prop="supplierId" />-->
        <el-table-column label="平台优惠卷名称" align="center" prop="platformName" />
        <el-table-column label="平台优惠卷价格" align="center" prop="platformPrice" />
        <el-table-column label="满减使用价格" align="center" prop="platformPriceMost" />
        <el-table-column label="优惠卷开始时间" align="center" prop="platformTimeStart" width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.platformTimeStart, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="优惠卷结束时间" align="center" prop="platformTimeEnd" width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.platformTimeEnd, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <!--<el-table-column label="类型" align="center" prop="platformType" />-->
        <el-table-column label="状态" align="center" prop="platformState" >
          <template slot-scope="scope">
            <el-tag
              :type="scope.row.platformState === '0' ? 'success' : 'danger'"
              disable-transitions>{{scope.row.platformState ==='0' ? '启动' : '禁用'}}</el-tag>
          </template>
        </el-table-column>
        <!--<el-table-column label="备用" align="center" prop="platformRemark" />-->
        <el-table-column label="最后修改时间" align="center" prop="modifiedTime" width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.modifiedTime, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['super:coupon:edit']"
            >修改</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['super:coupon:remove']"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />

      <!-- 添加或修改平台优惠卷对话框 -->
      <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
        <el-form ref="form" :model="form" :rules="rules" label-width="180px">
          <!--<el-form-item label="店铺ID" prop="supplierId">-->
            <!--<el-input v-model="form.supplierId" placeholder="请输入店铺ID" />-->
          <!--</el-form-item>-->
          <el-form-item label="平台优惠卷名称" prop="platformName">
            <el-input v-model="form.platformName" placeholder="请输入平台优惠卷名称" />
          </el-form-item>
          <el-form-item label="平台优惠卷价格" prop="platformPrice">
            <el-input v-model="form.platformPrice" placeholder="请输入平台优惠卷价格" />
          </el-form-item>
          <el-form-item label="满减使用价格" prop="platformPriceMost">
            <el-input v-model="form.platformPriceMost" placeholder="请输入满减使用价格" />
          </el-form-item>
          <el-form-item label="优惠卷开始时间" prop="platformTimeStart">
            <el-date-picker clearable size="small" style="width: 200px"
                            v-model="form.platformTimeStart"
                            type="date"
                            value-format="yyyy-MM-dd"
                            placeholder="选择优惠卷开始时间">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="优惠卷结束时间" prop="platformTimeEnd">
            <el-date-picker clearable size="small" style="width: 200px"
                            v-model="form.platformTimeEnd"
                            type="date"
                            value-format="yyyy-MM-dd"
                            placeholder="选择优惠卷结束时间">
            </el-date-picker>
          </el-form-item>
          <!--<el-form-item label="类型" prop="platformType">-->
            <!--<el-select v-model="form.platformType" placeholder="请选择类型">-->
              <!--<el-option label="请选择字典生成" value="" />-->
            <!--</el-select>-->
          <!--</el-form-item>-->
          <el-form-item label="状态" prop="platformState">
            <!--<el-input v-model="form.platformState" placeholder="请输入0 启用 1 禁用" />-->
            <el-select  v-model="form.platformState">
              <el-option label="启用" value="0" />
              <el-option label="禁用" value="1" />
            </el-select>
          </el-form-item>
          <!--<el-form-item label="备用" prop="platformRemark">-->
            <!--<el-input v-model="form.platformRemark" placeholder="请输入备用" />-->
          <!--</el-form-item>-->
          <!--<el-form-item label="最后修改时间" prop="modifiedTime">-->
            <!--<el-date-picker clearable size="small" style="width: 200px"-->
                            <!--v-model="form.modifiedTime"-->
                            <!--type="date"-->
                            <!--value-format="yyyy-MM-dd"-->
                            <!--placeholder="选择最后修改时间">-->
            <!--</el-date-picker>-->
          <!--</el-form-item>-->
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </el-dialog>

</el-card>
    </div>
</template>

<script>
  import { listCoupon, getCoupon, delCoupon, addCoupon, updateCoupon, exportCoupon } from "@/api/super/coupon";


  export default {
        name: "coupon",
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 平台优惠卷表格数据
        couponList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          supplierId: null,
          platformName: null,
          platformPrice: null,
          platformPriceMost: null,
          platformTimeStart: null,
          platformTimeEnd: null,
          platformType: null,
          platformState: null,
          platformRemark: null,
          modifiedTime: null
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          platformName: [
            { required: true, message: "平台优惠卷名称不能为空", trigger: "blur" }
          ],
          platformPrice: [
            { required: true, message: "平台优惠卷价格不能为空", trigger: "blur" }
          ],
          platformPriceMost: [
            { required: true, message: "满减使用价格不能为空", trigger: "blur" }
          ],
          platformTimeStart: [
            { required: true, message: "优惠卷开始时间不能为空", trigger: "blur" }
          ],
          platformTimeEnd: [
            { required: true, message: "优惠卷结束时间不能为空", trigger: "blur" }
          ],
          platformState: [
            { required: true, message: "0 启用 1 禁用不能为空", trigger: "blur" }
          ],
          modifiedTime: [
            { required: true, message: "最后修改时间不能为空", trigger: "blur" }
          ]
        }
      };
    },
    created() {
      this.getList();
    },
    methods: {
      /** 查询平台优惠卷列表 */
      getList() {
        this.loading = true;
        listCoupon(this.queryParams).then(response => {
          this.couponList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          platformId: null,
          supplierId: null,
          platformName: null,
          platformPrice: null,
          platformPriceMost: null,
          platformTimeStart: null,
          platformTimeEnd: null,
          platformType: null,
          platformState: null,
          platformRemark: null,
          modifiedTime: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.platformId)
        this.single = selection.length!==1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加平台优惠卷";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const platformId = row.platformId || this.ids
        getCoupon(platformId).then(response => {
          this.form = response.data;
        this.open = true;
        this.title = "修改平台优惠卷";
      });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.platformId != null) {
              updateCoupon(this.form).then(response => {
                if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
            } else {
              addCoupon(this.form).then(response => {
                if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const platformIds = row.platformId || this.ids;
        this.$confirm('是否确认删除平台优惠卷编号为"' + platformIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delCoupon(platformIds);
        }).then(() => {
          this.getList();
        this.msgSuccess("删除成功");
      }).catch(function() {});
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams;
        this.$confirm('是否确认导出所有平台优惠卷数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportCoupon(queryParams);
        }).then(response => {
          this.download(response.msg);
      }).catch(function() {});
      }
    }
    }
</script>

<style scoped>

</style>
